package org.cycuchy.hp.components {
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filters.DropShadowFilter;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.effects.Zoom;
	import mx.events.ToolTipEvent;
	
	import org.cycuchy.hp.event.PreloadingImageEvent;

	public class KapstelekImageCanvas extends Canvas {
		
		private var canvas:SquaresPreloadingCanvas;
		private var image:Image;
		private var zoom:Zoom;
		
		public function KapstelekImageCanvas(zoomWidthTo:Number, zoomHeightTo:Number, imgWidth:Number, imgHeight:Number, imgSource:String, toolTipValue:String)	{
			super();
			
			zoom = new Zoom();
			zoom.zoomWidthTo = zoomWidthTo;
			zoom.zoomHeightTo = zoomHeightTo;
			zoom.zoomWidthFrom = 1.0;
			zoom.zoomHeightFrom = 1.0;
			zoom.duration = 300;
			zoom.originX = imgWidth/2;
			zoom.originY = imgHeight/2;
			
			image = new Image();
			image.source = imgSource;
			image.width = imgWidth;
			image.height = imgHeight;
			image.filters = createFilters();
			image.addEventListener(Event.COMPLETE, onComplete);
			image.addEventListener(ProgressEvent.PROGRESS, onProgress);

//			addEventListener(Event.INIT, onLoad);
				
			addEventListener(PreloadingImageEvent.LOAD, onLoad);
			addEventListener(PreloadingImageEvent.CANT_ZOOM, cantZoom);
			addEventListener(PreloadingImageEvent.CAN_ZOOM, canZoom);
			addEventListener(MouseEvent.ROLL_OVER, doZoom);
			addEventListener(MouseEvent.ROLL_OUT, doZoom);
			
			addEventListener(ToolTipEvent.TOOL_TIP_CREATE, tt);
			toolTip = toolTipValue; 
		}
		
		override protected function createChildren():void {
			super.createChildren();
			canvas = new SquaresPreloadingCanvas();
			addChild(canvas);
			
//			addChild(image);
		}
		
		//http://blog.paranoidferret.com/index.php/2007/11/30/the-flex-drop-shadow
		private function createFilters():Array {
			var filters:Array = new Array();
		    var dFilter1:DropShadowFilter = new DropShadowFilter();
		    var dFilter2:DropShadowFilter = new DropShadowFilter();
		    
		    dFilter1.alpha = 0.4;
		    dFilter1.angle = 45;
		    dFilter1.distance = 2;
//		    dFilter1.blurX = 10;
//		    dFilter1.blurY = 10;
		    dFilter1.strength = 2;
		    dFilter1.inner = true;
		    filters.push(dFilter1);

		    dFilter2.alpha = 0.4;
		    dFilter2.angle = 45;
		    dFilter2.distance = 2;
//		    dFilter1.blurX = 10;
//		    dFilter1.blurY = 10;
		    dFilter2.strength = 2;
		    dFilter2.inner = false;
		    filters.push(dFilter2);
		    
		    return filters; 
		}
		
		private function doZoom(event:MouseEvent):void {
			if (_cantZoom) {
				return;
			}
			
			if (event.type == MouseEvent.ROLL_OVER) {
				parent.setChildIndex(this, parent.numChildren - 1);
			}
						
			if (zoom.isPlaying) {
				zoom.reverse();					
			} else {
				zoom.play([event.target], event.type == MouseEvent.ROLL_OUT ? true : false);					
			}
		}
		
		private var _cantZoom:Boolean = true;
		
		private function cantZoom(event:PreloadingImageEvent):void {
//			trace("pic cant");
			_cantZoom = true;
		}

		private function canZoom(event:PreloadingImageEvent):void {
//			trace("pic can");
			_cantZoom = false;
		}
		
		private function onProgress(event:ProgressEvent):void {
//			trace(image.source + " " + event.bytesLoaded + " " + event.bytesTotal);
			if (canvas != null) {
				canvas.progress(event.bytesLoaded, event.bytesTotal);
			}
		}
		
		private function onComplete(event:Event):void {
//			trace("onComplete 0");
			if (canvas) {
//				trace("onComplete 1");
				canvas.visible = false;
				removeChild(canvas);
				canvas = null;
			}
			_cantZoom = false;
			(image.content as Bitmap).smoothing = true;
		}	
		
		private function onLoad(event:Event):void	{
//			trace("onLoad");
//			canvas = new SquaresPreloadingCanvas();
//			addChild(canvas);
			addChild(image);				
		}
		
		private function tt(event:ToolTipEvent):void {
			event.toolTip = new HPToolTip(".kapstelekToolTip");
		}

	}
	
}